package vip.xiaomaoxiaoke.lock.impl;


import vip.xiaomaoxiaoke.lock.FieldLock;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class JvmFieldLockImpl<T> implements FieldLock<T> {
    private volatile Map<T, Lock> map = new ConcurrentHashMap<>();

    @Override
    public void lock(T key) throws InterruptedException {
        Lock lock = map.computeIfAbsent(key, (s) -> new ReentrantLock());
        lock.lock();
    }
    @Override
    public void unlock(T key) {
        Lock remove = map.get(key);
        remove.unlock();
    }
}
